package programmercarl.贪心.C11;

class Solution {
    public int canCompleteCircuit(int[] gas, int[] cost) {
        int curr = 0;
        int global = 0;
        int index = 0;
        //局部最优推全局最优
        for (int i = 0; i < gas.length; i++) {
            curr += gas[i] - cost[i];
            global += gas[i] - cost[i];
            if (curr < 0) {
                index = (i+1) % gas.length;
                curr = 0;
            }
        }
        return global >= 0 ? index : -1;
    }
}